package com.heg.api.control;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heg.api.service.SmsModuleService;
import com.heg.api.service.UserModuleService;
import com.heg.api.tool.RequestModel;
import com.heg.api.tool.ResponseCode;
import com.heg.api.tool.ResponseModel;
import com.heg.api.util.PublicMethodsUtils;
import com.heg.bk.entity.HegMobileUser;

/**
 * 用户模块api接口
 * @author 邓绪招
 * @time 2016-04-04
 * @version 1.0.1
 */
@RequestMapping(value = "/hegapi/user")
@Controller("userModuleAction")
public class UserModuleAction {
	
	@Resource
	private UserModuleService moduleService;
	
	@Resource
	private SmsModuleService smsModuleService;
	
	/**
	 * 用户注册api
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/nureg", method = RequestMethod.POST)
	public ResponseModel reg_new_user(@RequestBody String params){
		Map<String, Object> paramsMaps = RequestModel.getRequestParams(params);
		String telnumber = paramsMaps.get("telnumber").toString();
		String smsId = paramsMaps.get("smsId").toString();
		Map<String, Object> smsObj= smsModuleService.get_sms_toSmsId(smsId);
		if(smsObj != null){
			String sendTime = smsObj.get("ctime").toString();
			if(PublicMethodsUtils.verify_startEndTime(sendTime)){
				//判定手机号码是否被注册
				if(moduleService.verification_isreg(telnumber)){
					String codes = paramsMaps.get("seccodes").toString();
					String password = paramsMaps.get("password").toString();
					//设备信息
					String deviceVersion = paramsMaps.get("deviceVersion").toString();
					String deviceType = paramsMaps.get("deviceType").toString();
					String deviceId = paramsMaps.get("deviceId").toString();
					String deviceKey = paramsMaps.get("deviceKey").toString();
					//经纬度
					String lng = paramsMaps.get("lng").toString();
					String lat = paramsMaps.get("lat").toString();
					
					//推送通道
					String userId = paramsMaps.get("userId").toString();
					String channelId = paramsMaps.get("channelId").toString();
					
					HegMobileUser user = new HegMobileUser();
					user.setTelnumber(telnumber);
					user.setPassword(password);
					Map<String, Object> userMaps = moduleService.regUser(telnumber, password, codes, deviceVersion, deviceType, deviceKey, 
							deviceId, lat, lng, userId, channelId, null, null);
					if(userMaps != null && userMaps.size() > 0){
						return new ResponseModel(ResponseCode.REQUEST_SUCCESS, "sucess", userMaps);
					} else {
						return new ResponseModel(ResponseCode.SYSTEM_ERROR, "error", "系统错误");
					}
				} else {
					return new ResponseModel(ResponseCode.REQUEST_USER_REG_FAILURE, "error", "用户已经注册!");
				}
			} else {
				return new ResponseModel(ResponseCode.REQUEST_SMS_EXPIRED_FAILURE, "error", "验证码已过期!");
			}
		} else {
			return new ResponseModel(ResponseCode.REQUEST_SMS_EXIST_FAILURE, "error", "验证码不存在!");
		}
	}
	
	/**
	 * 用户登录api
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ResponseModel user_login(@RequestBody String params){
		Map<String, Object> paramsMaps = RequestModel.getRequestParams(params);
		Map<String, Object> retMaps = new HashMap<String, Object>();
		String telnumber = paramsMaps.get("telnumber").toString();
		String password = paramsMaps.get("password").toString();
		String tokenCode = paramsMaps.get("tokenCode").toString();
		String lat = paramsMaps.get("lat").toString();
		String lng = paramsMaps.get("lng").toString();
		String userId = paramsMaps.get("userId").toString();
		String channelId = paramsMaps.get("channelId").toString();
		Object obj;   
		if(tokenCode != null && !"".equals(tokenCode) && !"null".equals(tokenCode)){
			obj = moduleService.appUser_non_firstLogin(tokenCode, lat, lng, userId, channelId);
		} else {
			obj = moduleService.appUser_firstLogin(telnumber, password, lat, lng, userId, channelId);
		}
		if(obj != null){
			return new ResponseModel(ResponseCode.REQUEST_SUCCESS, "success", obj);
		} else {
			return new ResponseModel(ResponseCode.REQUEST_UNAME_PWORD_FAILURE, "error", "�û����������!");
		}
	}

}
